package com.seaengine.resourcemanager.loadtool
{
	import com.seaengine.GameConfig;
	
	import flash.events.Event;
	import flash.events.EventDispatcher;
	import flash.events.IOErrorEvent;
	import flash.net.URLLoaderDataFormat;
	import flash.net.URLRequest;
	import flash.system.ApplicationDomain;
	import flash.system.LoaderContext;
	import flash.system.SecurityDomain;
	import flash.utils.Dictionary;
	import flash.utils.getTimer;
	

	/**
	 * 加载器
	 */	
	public class LoadUtils extends EventDispatcher
	{
		
		//--------------------------------------------------------------------------
		//
		//  variable
		//
		//--------------------------------------------------------------------------
		/**文件路径出错*/
		public static const FILE_URL_ERROR:String = "FILE_URL_ERROR";
		
		/**文件加载完成*/
		public static const FILE_LOADER_COMPLETE:String = "file_loader_complete";
		
		/**XML加载列表*/
		private var urlLoaderList:Array;
		
		/**显示对象加载列表*/
		private var loaderList:Array;
		
		/**当前加载文件数量*/
		private var currentCount:uint;
		
		/**要加载文件数量*/
		private var totalCount:uint;
		
		/**加载批次数组*/
		private var loadingBatch:Dictionary;
		
		public function LoadUtils()
		{
			urlLoaderList = new Array();
			loaderList = new Array();
			loadingBatch = new Dictionary();
		}
		
		//--------------------------------------------------------------------------
		//
		//  public
		//
		//--------------------------------------------------------------------------
		/**
		 * 加载显示对象
		 * @param url  需要加载的路径
		 * @param fun  回调方法(回调时候有参数)
		 * @param param 可变参数列表是数组(回调参数顺序与你传递时候一样)
		 */	
		public function addDisplayURL(url:String,fun:Function,...param):void
		{
			var obj:Object = new Object();
			if(url.indexOf("http:") == -1 && url.indexOf("GYGame") == -1 && url.indexOf("GameSelectSex") == -1)
			{
				obj["url"] = GameConfig.m_ResConfig + url 
			}
			else
			{
				obj["url"] = url 
			}
			obj["fun"] = fun;
			obj["param"] = parseArray(param);
			loaderList.push(obj);
		}
		
		/**
		 * 加载XML
		 * @param url  需要加载的路径
		 * @param fun  回调方法(回调时候有参数)
		 * @param param 可变参数列表是数组(回调参数顺序与你传递时候一样)
		 */	
		public function addXMLURL(url:String,fun:Function,...param):void
		{
			var obj:Object = new Object();
			obj["url"] = GameConfig.m_ResConfig + url
			obj["fun"] = fun;
			obj["param"] = parseArray(param);
			urlLoaderList.push(obj);
		}
		/**
		 * 开始加载
		 */		
		public function load():void
		{
			var lc:LoaderContext = new LoaderContext();
			lc.applicationDomain = ApplicationDomain.currentDomain;    //这个是关键
			lc.checkPolicyFile = true;
			var timer:int = getTimer();
			for(var a:uint = 0; a < loaderList.length; a++)
			{
				var obj:Object = loaderList[a];
				var urlRequest:URLRequest = new URLRequest(obj["url"]);
				var loader:LoaderOverride = new LoaderOverride(urlRequest.url);
				loader.load(urlRequest,lc);
				loader.timeStamp = timer;
				loader.loaderData.method = obj["fun"];
				loader.loaderData.paramList = obj["param"];
				loader.contentLoaderInfo.addEventListener(Event.COMPLETE,disCompleteHandler);
				loader.contentLoaderInfo.addEventListener(IOErrorEvent.IO_ERROR,disErrorHandler);
				obj = null;
				urlRequest = null;	
				loader = null;
			}
			for(var b:uint = 0; b < urlLoaderList.length; b++)
			{
				var obj1:Object = urlLoaderList[b];
				var urlRequest1:URLRequest = new URLRequest(obj1["url"]);
				var urlLoader:URLLoaderOverride = new URLLoaderOverride(urlRequest1.url);
				if(urlRequest1.url.indexOf(".zip") != -1)
				{
					urlLoader.dataFormat = URLLoaderDataFormat.BINARY;
				}
				urlLoader.load(urlRequest1);
				urlLoader.timeStamp = timer;
				urlLoader.loaderData.method = obj1["fun"];
				urlLoader.loaderData.paramList = obj1["param"];
				urlLoader.addEventListener(Event.COMPLETE,xmlCompleteHandler);
				urlLoader.addEventListener(IOErrorEvent.IO_ERROR,xmlErrorHandler);
				obj1 = null;
				urlRequest1 = null;	
				urlLoader = null;
			}
			totalCount = loaderList.length + urlLoaderList.length;
			currentCount = 0;
			loaderList.length = 0;
			urlLoaderList.length = 0;
			loadingBatch[timer] = {currentCount:currentCount,totalCount:totalCount};
		}
		
		//--------------------------------------------------------------------------
		//
		//  private
		//
		//--------------------------------------------------------------------------
		/**
		 * 解析数组
		 */		
		private function parseArray(param:Array):Array
		{
			var arr:Array = new Array();
			for each(var obj:Object in param)
			{
				if(obj is Array)
				{
					for each(var obj$:Object in obj)
					{
						arr.push(obj$);
					}
				}
				else
				{
					arr.push(obj);
				}
			}
			return arr;
		}
		
		/**
		 * 当前加载文件数量判断
		 * @param timeStamp 加载批次时间戳
		 */		
		private function currentCountJudge(timeStamp:int):void
		{
			var obj:Object = loadingBatch[timeStamp];
			if(obj != null)
			{
				obj.currentCount++;
				if(obj.currentCount == obj.totalCount)
				{
					delete loadingBatch[timeStamp];
					this.dispatchEvent(new Event(FILE_LOADER_COMPLETE));
				}
			}
		}
		//--------------------------------------------------------------------------
		//
		//  event
		//
		//--------------------------------------------------------------------------
		/**
		 * XML加载完成 
		 */		
		private function xmlCompleteHandler(e:Event):void
		{
			var urlLoader:URLLoaderOverride = URLLoaderOverride(e.target);
//			if(urlLoader.url.indexOf(".zip") != -1)
//			{
//				urlLoader.loaderData.data = nochump.util.zip.ZipUtil.uncompress(e.currentTarget.data);
//			}
//			else
//			{
				urlLoader.loaderData.data = e.currentTarget.data;
//			}
			var iLoader:ILoader = urlLoader.iLoader;
			urlLoader.removeEventListener(Event.COMPLETE,xmlCompleteHandler);
			iLoader.method.call(iLoader.method,iLoader);
			currentCountJudge(urlLoader.timeStamp);
		}
		
		/**
		 * 显示对象加载完成 
		 */		
		private function disCompleteHandler(e:Event):void
		{
			var loader:LoaderOverride = LoaderOverride(e.target.loader);
			loader.loaderData.data = e.currentTarget;
			var iLoader:ILoader = loader.iLoader;
			loader.contentLoaderInfo.removeEventListener(Event.COMPLETE,disCompleteHandler);
			iLoader.method.call(iLoader.method,iLoader);
			currentCountJudge(loader.timeStamp);
		}
		
		/**
		 * XML加载错误信息
		 */		
		private function xmlErrorHandler(e:Event):void
		{
			var urlLoader:URLLoaderOverride = URLLoaderOverride(e.target);
			this.dispatchEvent(new Event(FILE_URL_ERROR));
		}
		
		/**
		 * 显示对象加载错误信息
		 */		
		private function disErrorHandler(e:Event):void
		{
			var loader:LoaderOverride = LoaderOverride(e.target.loader);
			this.dispatchEvent(new Event(FILE_URL_ERROR));
		}
	}
}